package com.woniu.petkonwledge.controller;

import com.alibaba.fastjson.JSONArray;
import com.woniu.common.entity.QuestionBank;
import com.woniu.common.entity.ResponseResult;
import com.woniu.common.entity.Study;
import com.woniu.petkonwledge.service.QuestionBankService;
import com.woniu.petkonwledge.service.StudyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("study")
public class StudyController {
    //判断对错以及将做题信息添加到数据库中
    @Autowired
    StudyService studyService;
    @Autowired
    QuestionBankService questionBankService;
    //判断并添加学习信息
    @ResponseBody
    @RequestMapping("/addStudyAndIsTrue")
    public ResponseResult<List<Integer>> addStudyAndIsTrue(@RequestBody String str){
        List<Study> studies = JSONArray.parseArray(str, Study.class);
        ResponseResult<List<Integer>> result = new ResponseResult<>();
        List<Integer> msg = new ArrayList<>();
        for (int i = 0;i < studies.size();i ++){
            Study study = studies.get(i);
            System.out.println(study.getUser().getId());
            QuestionBank questionBank = questionBankService.getQuestionById(study.getQuestionBank().getId());
            String answer = questionBank.getAnswer();
            String[] answers = answer.split(" ");
            if (answers[0].equals(study.getMyoption())){
                msg.add(1);
            }else {
                msg.add(2);
            }
            studyService.insertStudy(studies.get(i));
        }
        result.setCode(200);
        result.setMessage("成功");
        result.setData(msg);
        return result;
    }
    @ResponseBody
    @RequestMapping("/getStudyByU_id")
    public ResponseResult<List<Study>> getStudyByU_id(int u_id){

        ResponseResult<List<Study>> result = new ResponseResult<>();
        List<Study> studies = studyService.getStudyByU_id(u_id);
        result.setData(studies);
        result.setMessage("查询成功");
        result.setCode(200);
        return result;
    }
}
